using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net; 
using System.Text.RegularExpressions;
using System.IO;

/// <summary>
/// Summary description for HtmlUtility
/// </summary>
public class HtmlUtility
{
    static Regex tag = new Regex("<.*?>");

    public static string GetHtml(string url)
    {
        return GetHtml(url, null);
    }

    public static int GetResponseLength(string url)
    {
        WebClient client = new WebClient();
        client.Headers.Set("Pragma", "no-cache");
        byte[] buf = client.DownloadData(url);
        if (buf == null)
        {
            return 0;
        }
        return buf.Length;
    }

    static string GetHtml(string url, Encoding encoding)
    {
        WebClient client = new WebClient();
        client.Headers.Set("Pragma", "no-cache");
        byte[] buf = client.DownloadData(url);
        client.Dispose();
        if (encoding != null) return encoding.GetString(buf);
        string html = Encoding.UTF8.GetString(buf);
        encoding = GetEncoding(html);
        if (encoding == null || encoding == Encoding.UTF8) return html;
        return encoding.GetString(buf);
    }

    static Encoding GetEncoding(string html)
    {
        string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
        string charset = Regex.Match(html, pattern).Groups["charset"].Value;
        try { return Encoding.GetEncoding(charset); }
        catch (ArgumentException) { return null; }
    }

    public static string PostHtml(string url, Encoding encoding, string data)
    {
        byte[] dataBuffer = Encoding.ASCII.GetBytes(data);

        WebClient client = new WebClient();
        byte[] buf = client.UploadData(url, dataBuffer);

        client.Dispose();

        if (encoding != null)
        {
            return encoding.GetString(buf);
        }
        string html = Encoding.UTF8.GetString(buf);
        encoding = GetEncoding(html);
        if (encoding == null || encoding == Encoding.UTF8) return html;

        return encoding.GetString(buf);
    }

    // 
    static public int GetHtmlWordCount(string content, int adjust)
    {
        MatchCollection mc = tag.Matches(content);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mc.Count - 1; i++)
        {
            int last = mc[i].Index + mc[i].Length;
            sb.Append(content.Substring(last, mc[i + 1].Index - last));
        }
        string s = sb.ToString();
        // 40 is the prefix rubbish string.
        return s.Length - adjust;
    }


    public static string GetDataByUrl(string url)
    {
        Stream stream = null;
        StreamReader reader = null;
        string data = null;
        WebClient webClient = new WebClient();
        try
        {
            stream = webClient.OpenRead(url);

            Encoding gbEncoding = Encoding.GetEncoding("GBK");
            reader = new StreamReader(stream, gbEncoding);
            data = reader.ReadToEnd();
        }
        catch
        {
        }
        finally
        {
            if (webClient != null)
            {
                webClient.Dispose();
                webClient = null;
            }

            if (reader != null)
            {
                reader.Close();
                reader.Dispose();
                reader = null;
            }
            if (stream != null)
            {
                stream.Close();
                stream.Dispose();
                stream = null;
            }
        }

        return data;
    }
}
